package client.states;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyEvent;

import com.jpemartin.jgt.KeyboardStatus;

import client.DoublyLinkedList;
import client.IGameState;
import client.GraphicsLoader;
import client.Link;
import client.StateContext;
import client.commands.CreditsCommand;
import client.commands.ICommand;
import client.commands.LoadGameCommand;
import client.commands.NewGameCommand;

public class TitleScreen implements IGameState {
	private DoublyLinkedList commands;
	private Link selectedCommand;
	private Image backgroundImg;
	
	public TitleScreen() {
		commands = new DoublyLinkedList();
		backgroundImg = GraphicsLoader.loadImage("TitleScreen.png");
		ICommand newGame = new NewGameCommand();
		commands.insertFirst(newGame);
		ICommand loadGame = new LoadGameCommand();
		commands.insertLast(loadGame);
		ICommand credits = new CreditsCommand();
		commands.insertLast(credits);
		selectedCommand = commands.head();
	}
	
	@Override
	public void paint(Graphics g, StateContext stateContext, int GameTime) {
		g.drawImage(backgroundImg, 0, 0, null);
		int y = 125;
		if (commands != null) {
			Link current = commands.head();
			while (current != null) {
				
				if (current.equals(selectedCommand)) {
					g.setColor(new Color(0, 0, 220));
					g.fillRect(250, y-25, 100, 50);
					g.setColor(Color.WHITE);
					g.drawRect(250, y-25, 100, 50);
				}
				g.setColor(Color.WHITE);
				g.setFont(GraphicsLoader.getSmallFont());
				g.drawString(current.dData.toString(), 255, y);
				y += 50;
				current = current.next;
			}
		}
	}

	@Override
	public void processKeyHit(StateContext stateContext, KeyboardStatus status) {
		if(status.key[KeyEvent.VK_RIGHT]){
			if (selectedCommand.next != null) {
				selectedCommand = selectedCommand.next;
				status.key[KeyEvent.VK_RIGHT]=false;
			}
		} else
		if(status.key[KeyEvent.VK_DOWN]){
			if (selectedCommand.next != null) {
				selectedCommand = selectedCommand.next;
				status.key[KeyEvent.VK_DOWN]=false;
			}
		} else
		if(status.key[KeyEvent.VK_LEFT]){
			if (selectedCommand.previous != null) {
				selectedCommand = selectedCommand.previous;
				status.key[KeyEvent.VK_LEFT]=false;
			}
		} else
		if(status.key[KeyEvent.VK_UP]){
			if (selectedCommand.previous != null) {
				selectedCommand = selectedCommand.previous;
				status.key[KeyEvent.VK_UP]=false;
			}
		} else 
		if(status.key[KeyEvent.VK_ENTER]){
			status.key[KeyEvent.VK_ENTER]=false;
			ICommand comm = (ICommand) selectedCommand.dData;
			comm.execute(stateContext);
		}
	}

}
